package com.example.ymm.modules1.service.impl.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.exception.YmException;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.my.user.DbCategory;
import com.example.ymm.modules1.entity.my.user.DbGood;
import com.example.ymm.modules1.mapper.my.user.DbGoodMapper;
import com.example.ymm.modules1.query.DbGoodQueryCriteria;
import com.example.ymm.modules1.service.user.DbCategoryService;
import com.example.ymm.modules1.service.user.DbGoodService;
import com.example.ymm.util.QueryHelpPlus;
import com.example.ymm.util.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
@Slf4j
public class DbGoodServiceImpl extends BaseServiceImpl<DbGoodMapper, DbGood> implements DbGoodService {

    @Autowired
    DbCategoryService dbCategoryService;

    @Override
    public TableList<DbGood> list(DbGoodQueryCriteria criteria, YmPageDto pageDto) {
        IPage<DbGood> page = new Page<DbGood>(pageDto.getPage(),pageDto.getPageSize());
        QueryWrapper predicate = QueryHelpPlus.getPredicate(DbGood.class, criteria);
        predicate.orderByDesc("create_time");
        baseMapper.selectPage(page, predicate);
        TableList<DbGood> tableList=new TableList(page);
        return tableList;
    }

    public List<DbGood> queryAll(DbGoodQueryCriteria criteria) {
        QueryWrapper predicate = QueryHelpPlus.getPredicate(DbGood.class, criteria);
        predicate.orderByDesc("create_time");
        List<DbGood> result =baseMapper.selectList(predicate);
        return result;
    }


    @Override
    public DbGood findByGoodName(String goodName) {
        DbGoodQueryCriteria criteria=new DbGoodQueryCriteria();
        criteria.setGoodName(goodName);
        List<DbGood> dbGoods = queryAll(criteria);
        if(dbGoods.size()>0){
            return dbGoods.get(0);
        }
        return null;
    }

    @Override
    public List<String> importGood(List<DbGood> list) {
        if(UtilValidate.isEmpty(list)){
            throw new YmException("导入数据不能为空!");
        }
        List<String> errors=new ArrayList<>();
        List<DbGood> addGoods=new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            DbGood good=list.get(i);
            if(UtilValidate.isEmpty(good.getGoodName())){
                errors.add("第"+(i+1)+"行 商品名称不能为空");
            }
            if(UtilValidate.isEmpty(good.getCategoryName())){
                errors.add("第"+(i+1)+"行 分类名称不能为空");
            }
            good.setGoodName(good.getGoodName().trim());
            good.setCategoryName(good.getCategoryName().trim());
            addGoods.add(good);
        }
        if(UtilValidate.isNotEmpty(errors)){
            return errors;
        }
        for (int i = 0; i < addGoods.size(); i++) {
            DbGood good = addGoods.get(i);
            DbGood byGoodName = findByGoodName(good.getGoodName());
            DbCategory byCategoryName = dbCategoryService.findByCategoryName(good.getCategoryName());
            if (byCategoryName != null) {
                good.setCategoryId(byCategoryName.getCategoryId());
            }
            if(byGoodName!=null){
                byGoodName.setCategoryId(good.getCategoryId());
                byGoodName.setStock(good.getStock());
                byGoodName.setMainImg(good.getMainImg());
                byGoodName.setDetail(good.getDetail());
                saveOrUpdate(byGoodName);
            }else{
                DbGood n_good = new DbGood();
                BeanUtils.copyProperties(good,n_good);
                n_good.setStatus(1);
                n_good.setIsDelete(0);
                saveOrUpdate(n_good);
            }
        }
        return errors;
    }
}
